#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

void test()
{
    vector<int> number = {1, 3, 5, 9, 7, 4, 6, 2, 10, 3};
    display(number);
    cout << "number.size() = " << number.size() << endl
         << "number.capacity() = " << number.capacity() << endl;

    cout << endl << "在vector的尾部进行插入与删除" << endl;
    number.push_back(33);
    display(number);
    cout << "number.size() = " << number.size() << endl
         << "number.capacity() = " << number.capacity() << endl;

    cout << endl << "在vector的任意位置进行插入元素" << endl;
    vector<int>::iterator it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, 100);
    display(number);
    cout << "number.size() = " << number.size() << endl
         << "number.capacity() = " << number.capacity() << endl;
    cout << "*it = " << *it << endl;

    //因为push_back每次只能插入一个元素，所以按照2 * capacity进行
    //扩容是没有任何问题的，但是insert进行插入元素的时候，插入元素
    //的个数是不确定的，所以不能按照2 * capacity进行扩容
    //
    //设置size() = m = 12, capacity() = n = 20, 待插入的元素个数t
    //1、当插入元素的个数t < n - m,此时就不会扩容 
    //2、当插入元素的个数 n - m < t < m,此时就按照2 * m进行扩容
    //3、当插入元素的个数 n - m < t, m < t < n,此时就按照t + m进行扩容
    //4、当插入元素的个数 n - m < t, t > n,此时就按照t + m进行扩容
    cout << endl << endl;
    //迭代器失效（重要）
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, 100, 200);
    display(number);
    cout << "number.size() = " << number.size() << endl
         << "number.capacity() = " << number.capacity() << endl;
    cout << "*it = " << *it << endl;

    cout << endl << endl;
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    vector<int> vec = {77, 99, 33, 55};
    number.insert(it, vec.begin(), vec.end());
    display(number);
    cout << "number.size() = " << number.size() << endl
         << "number.capacity() = " << number.capacity() << endl;
    cout << "*it = " << *it << endl;
}

int main(int argc, char **argv)
{
    test();
    return 0;
}
